<?php
require_once ("../models/config.php");
if (!securePage(__FILE__)) {
    // Forward to index page
    addAlert("danger", "Whoops, looks like you don't have permission to view that page.");
    header("Location: ../index.php");
    exit();
}

setReferralPage(getAbsoluteDocumentPath(__FILE__));
include '../newlib.php';
include '../lib/AppScope.pre.php';

$theUser = $loggedInUser->user_id;
$program = $_REQUEST["program"];
if ($program == "") {
	header("main.php");
} else if ($program == "-1") {
	// Nieuw programma
	$newname = $_REQUEST["newname"];
	// global $tmpConnection;
	$sql = 'INSERT INTO programs (titel, creator, created) VALUES (\'' . $newname . '\', \'' . $theUser . '\', now())';
	$result = $tmpConnection->exec($sql);
	$stm = $tmpConnection->prepare('SELECT LAST_INSERT_ID() as last_id');
	$stm->execute();
	$last_id = $stm->fetchAll();
	$program = intval($last_id[0]['last_id']);
	dispProgram($program, $newname, array());
} else {
	$sql = "select * from programs where id=" . $program;
	$stm = $tmpConnection->prepare($sql);
	$stm->execute();
	$rs = $stm->fetch(PDO::FETCH_ASSOC);

	$tmpLine = $rs["volgorde"];
	if (empty($tmpLine)) {
		$tmpLine = "";
	}
	$progname = $rs["titel"];
	$theArray = explode(";", $tmpLine);

	dispProgram($program, $progname, $theArray);
}

function dispProgram($program, $progname, $theArray) {
    global $tmpConnection, $PRODUCTNAME, $loggedInUser;
    $nav = 'nav-editprog.php';
    include 'header.php';
	echo <<<EOD
	<script language="JavaScript">
		function makeLive(id) {
			if (confirm("Klik op OK om dit programma op de $PRODUCTNAME te zetten. De geselecteerde pagina's worden dan uitgezonden."))
			self.location.href = "makelive.php&program=" + id;
		}
	</script>
	<h1>Programma <span class='label label-info'>$progname</span> aanpassen</h1>
	<hr />
	<div class="row">
		<div class='col-lg-3 col-md-4 col-sm-6 col-xs-12'>
            <div class='row'>
                <div class='col-xs-11'>
                    <div class='panel panel-primary'><div class='panel-heading'><h3 class='panel-title'>BEGIN</h3></div><div class='panel-body'></div></div>
                </div>
                <div class='col-xs-1 insert'><a title='Pagina invoegen' href='insert.php?position=-1&program=$program'><span class="fa fa-plus-square-o"></span><span class='fa fa-long-arrow-right'></span></a></div>
            </div>
		</div>
EOD;

	$stm = $tmpConnection->prepare('select * from vwcontent where id = :id');
    $counter = 1;
	foreach ($theArray as $t) {
        echo <<<EOD
		<div class='col-lg-3 col-md-4 col-sm-6 col-xs-12'>
		    <div class='row'>
                <div class='col-xs-11'>
EOD;
		$stm->execute(array('id' => intval($t)));
		$rs = $stm->fetch(PDO::FETCH_ASSOC);
        $tmpTitel = $rs['titel'];
        if ($tmpTitel == '') {
            $tmpTitel = '(geen titel)';
        }
        $panelStyle = empty($rs['schemaid']) ? 'panel-default' : 'panel-info';
        echo <<<EOD
                    <div class='panel $panelStyle'>
                        <div class='panel-heading'>
                            <h3 class='panel-title'><a href='editpage.php?position=$t&program=$program'>$tmpTitel</a></h3>
                            <a title='Verwijder pagina uit uitzending' class='remove right' href='remove.php?position=$t&program=$program'><span class='fa fa-times-circle-o'></a>
                        </div>
                        <div class='panel-body'>

EOD;

        if (!empty($rs['schemaid'])) {
			$bgcolor = "#6699ff";
			echo "<div class='row'>";
			$weekdays = array("ma", "di", "wo", "do", "vr", "za", "zo");
			foreach ($weekdays as $dag) {
				$xclass = $rs[$dag] != 1 ? 'badge-disabled' : 'badge-enabled';
                echo "<div class='col-xs-1'><span class='badge $xclass'>" . strtoupper(substr($dag, 0, 1)) . "</span></div>";
			}
			echo "</div><div class='row'><div class='text-center'><small>";
			if ($rs['van'] == "0000") {
                echo $rs['tot'] == "0000" ?  'alle datums' : "t/m " . asFormattedDate($rs['tot']);
			} else {
                echo $rs['tot'] == "0000" ? 'vanaf ' . asFormattedDate($rs['van']) : 'van ' . asFormattedDate($rs['van']) . " t/m " . asFormattedDate($rs['tot']);
			}
			echo '</small></div></div>';
		}
        echo <<<EOD
                        </div>
                    </div>
                </div>
                <div class='col-xs-1 insert'><a title='Pagina invoegen' href='insert.php?position=$t&program=$program'><span class="fa fa-plus-square-o"></span><span class='fa fa-long-arrow-right'></span></a></div>
		    </div>
		</div>
EOD;
        $counter++;
        if ($counter % 2 == 0) {
            echo "<div class='clearfix visible-sm'></div>";
        }
        if ($counter % 3 == 0) {
            echo "<div class='clearfix visible-md'></div>";
        }
        if ($counter % 4 == 0) {
            echo "<div class='clearfix visible-lg'></div>";
        }

	}
    echo <<<EOD
    <div class='col-lg-3 col-md-4 col-sm-6 col-xs-12'>
        <div class='row'>
            <div class='col-xs-11'>
                <div class='panel panel-primary'>
                    <div class='panel-heading'><h3 class='panel-title'>EINDE</h3></div>
                    <div class='panel-body'></div>
                </div>
            </div>
        </div>
    </div>
</div>
EOD;


	include 'footer.php';

}
include '../lib/AppScope.post.php';

?>